/**   
* 所属公司：稻普控股集团
* 项目名称：dpqc-parent-manager-party   
* 类名称：MessageController   
* 类描述：   
* 创建人：zhangpp
* 创建时间：2017年4月1日 上午9:26:19   
* 修改人：zhangpp
* 修改时间：2017年4月1日 上午9:26:19   
* 修改备注：   
* @version V1.0     
*/ 
package com.zjdiepu.www.manage.api.third;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.httpclient.HttpException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.google.gson.Gson;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.common.util.PartyUtil;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.ibiz.third.IBaseFormMessageService;
import com.zjdiepu.www.ibiz.third.IBaseShortMessageService;
import com.zjdiepu.www.model.third.dto.BaseFormMessage;
import com.zjdiepu.www.model.third.dto.BaseShortMessage;
import com.zjdiepu.www.model.third.dto.BaseToMessage;
import com.zjdiepu.www.model.third.vo.ShortMessageResultVo;

/**   
 * 所属公司：稻普控股集团
 * 项目名称：dpqc-parent-manager-party   
 * 类名称：MessageController   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年4月1日 上午9:26:19   
 * 修改人：zhangpp
 * 修改时间：2017年4月1日 上午9:26:19   
 * 修改备注：   
 * @version V1.0     
 */
@RestController
@RequestMapping("/MessageController")
@Api("短信接口")
public class MessageController extends BaseController{

	@Resource
	private IBaseShortMessageService ShortMessage = null;
	
	@Resource
	private IBaseFormMessageService iBaseFormMessageService = null;
	
	
	@ApiOperation(value = "短信调用", httpMethod = "GET", response = ShortMessageResultVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "phone",paramType = "query", dataType = "String", required = false,  defaultValue = ""),
    })
	
	//http://localhost:8080/integra/MessageController/queryShortMessage?secretKey=56i75pmu5o6n6IKh6ZuG5Zui&mobile=15158822934
	
	@RequestMapping(value="queryShortMessage",method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public Map<String, Object> queryShortMessage(@ApiIgnore BaseShortMessage baseShortMessage) throws HttpException, IOException
	{
		Map<String, Object> maps = success();
		try
		{
			if(null == baseShortMessage || ("").equals(baseShortMessage))
			{
				return super.errorMassage("baseShortMessage || 数据对象为空" , 100500);
			}
			
			if(null == baseShortMessage.getPhone() || ("").equals(baseShortMessage.getPhone()))
			{
				return super.errorMassage("phone  || 手机号码为空" , 100500);
			}
			
			int countRowsIn30Mins = ShortMessage.getCountRowsIn30MinsService(baseShortMessage);
			if(countRowsIn30Mins >= 3)
			{
				return super.errorMassage(baseShortMessage.getPhone() + "|| 您的操作过于频繁，请稍候再试!" , 100600);
			}
			String code = UUIDFactory.getCode();
			String result = PartyUtil.messageUtil(baseShortMessage.getPhone(), code);
			ShortMessageResultVo shortMessageResult = new Gson().fromJson(result, ShortMessageResultVo.class);
			
			if(shortMessageResult.getError_code() == 0)
			{
				shortMessageResult.setError_code(100100);
			}
			
			baseShortMessage.setPhoneCode(Integer.valueOf(code));
			baseShortMessage.setFlag(shortMessageResult.getError_code());
			baseShortMessage.setMessage(maps.get("message").toString());
			//短信表添加执行
			ShortMessage.saveUpdateAndDelete(baseShortMessage, OperateType.add);
			
			
			if(shortMessageResult.getError_code() == 205401)
			{
				return super.errorMassage("错误的手机号码", 205401);
			}
			
			if(shortMessageResult.getError_code() == 205402)
			{
				return super.errorMassage("错误的短信模板ID", 205402);
			}
			if(shortMessageResult.getError_code() == 205403)
			{
				return super.errorMassage("网络错误,请重试", 205403);
			}
			if(shortMessageResult.getError_code() == 205404)
			{
				return super.errorMassage("发送失败，具体原因请参考返回reason", 205404);
			}
			if(shortMessageResult.getError_code() == 205405)
			{
				return super.errorMassage("号码异常/同一号码发送次数过于频繁", 205405);
			}
			if(shortMessageResult.getError_code() == 205405)
			{
				return super.errorMassage("不被支持的模板", 205406);
			}
			if(shortMessageResult.getError_code() == 10001)
			{
				return super.errorMassage("错误的请求KEY", 10001);
			}
			if(shortMessageResult.getError_code() == 10002)
			{
				return super.errorMassage("该KEY无请求权限", 10002);
			}
			if(shortMessageResult.getError_code() == 10003)
			{
				return super.errorMassage("KEY过期", 10003);
			}
			if(shortMessageResult.getError_code() == 10004)
			{
				return super.errorMassage("错误的OPENID", 10004);
			}
			if(shortMessageResult.getError_code() == 10005)
			{
				return super.errorMassage("应用未审核超时，请提交认证", 10005);
			}
			if(shortMessageResult.getError_code() == 10007)
			{
				return super.errorMassage("未知的请求源", 10007);
			}
			if(shortMessageResult.getError_code() == 10008)
			{
				return super.errorMassage("被禁止的IP", 10008);
			}
			if(shortMessageResult.getError_code() == 10009)
			{
				return super.errorMassage("被禁止的KEY", 10009);
			}
			if(shortMessageResult.getError_code() == 10011)
			{
				return super.errorMassage("当前IP请求超过限制", 10011);
			}
			if(shortMessageResult.getError_code() == 10012)
			{
				return super.errorMassage("请求超过次数限制", 10012);
			}
			if(shortMessageResult.getError_code() == 10013)
			{
				return super.errorMassage("测试KEY超过请求限制", 10013);
			}
			if(shortMessageResult.getError_code() == 10014)
			{
				return super.errorMassage("系统内部异常(调用充值类业务时，请务必联系客服或通过订单查询接口检测订单，避免造成损失)", 10014);
			}
			if(shortMessageResult.getError_code() == 10020)
			{
				return super.errorMassage("接口维护", 10020);
			}
			if(shortMessageResult.getError_code() == 10021)
			{
				return super.errorMassage("接口停用", 10021);
			}
			
			maps.put("code", code);
			maps.put("shortMessageResult", shortMessageResult);
		}
		catch (Exception e)
		{
			
			return super.errorMassage(e, "出错了    调取短信  ||  queryShortMessage");
		}
		
		return maps;
	}
	
	//http://localhost:8080/integra/MessageController/saveMessage?formId=1&dp_flag=1&remarks=shouji&content=shouji&toId=2
	@RequestMapping(value="saveMessage",method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public Map<String, Object> saveMessage(@ApiIgnore BaseFormMessage baseFormMessage,BaseToMessage baseToMessage) throws HttpException, IOException
	{
		Map<String, Object> maps = success();
		try 
		{
			if(null == baseFormMessage || ("").equals(baseFormMessage))
			{
				return super.errorMassage("baseFormMessage || 数据对象为空" , 100500);
			}
			
			if(null == baseToMessage || ("").equals(baseToMessage))
			{
				return super.errorMassage("baseToMessage || 数据对象为空" , 100500);
			}
			
			iBaseFormMessageService.save(baseFormMessage, baseToMessage);
		} 
		catch (Exception e) 
		{
			
			return super.errorMassage(e, "出错了    消息通知 ||  saveMessage");
		}
		return maps;
	}
}
